<?php

namespace Cframe;

class YException {
    /**
     * 注册异常处理
     */
    public static $localName = array(
        E_COMPILE_ERROR => 'PHP Compile Error',
        E_COMPILE_WARNING => 'PHP Compile Warning',
        E_CORE_ERROR => 'PHP Core Error',
        E_CORE_WARNING => 'PHP Core Warning',
        E_DEPRECATED => 'PHP Deprecated Warning',
        E_ERROR => 'PHP Fatal Error',
        E_NOTICE => 'PHP Notice',
        E_PARSE => 'PHP Parse Error',
        E_RECOVERABLE_ERROR => 'PHP Recoverable Error',
        E_STRICT => 'PHP Strict Warning',
        E_USER_DEPRECATED => 'PHP User Deprecated Warning',
        E_USER_ERROR => 'PHP User Error',
        E_USER_NOTICE => 'PHP User Notice',
        E_USER_WARNING => 'PHP User Warning',
        E_WARNING => 'PHP Warning',
        4016 => 'Customer`s Error',
    );

    public static function register(){
        ini_set("display_errors",'On');
        error_reporting(E_ALL || ~E_NOTICE);
        set_error_handler([__CLASS__,"yfError"]);
        set_exception_handler([__CLASS__,'yfException']);
        register_shutdown_function([__CLASS__,'yfShutdown']);
    }

    public static function yfError($type, $msg, $file = '', $line = 0, $content = []){
        $type = isset(static::$localName[$type])?static::$localName[$type]:static::$localName[4016];
        if (!strpos($type,"Warning")) {
            echo static::consoleConvertError($msg, $type, $line, $file);
        }
    }

    public static function yfException($e){
        $sapi_type  =  php_sapi_name ();
        if ( substr ( $sapi_type ,  0 ,  3 ) ==  'cgi' ) {
            echo static::consoleConvertException($e);
        } else {
            echo static::htmlConvertEx($e);
        }
    }


    protected static function isFatal($type){
        return in_array($type, array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE));
    }

    public static function yfShutdown(){
        $e = error_get_last();
        if (is_array($e) && static::isFatal($e['type'])){
        }
        if (class_exists(__NAMESPACE__."\\Log")){
            Log::save();
        }
        if (class_exists(__NAMESPACE__."\\Hook")){
            Hook::save();
        }
    }

    private static function consoleConvertException($e){

    }
    private static function consoleConvertError($message,$info,$line,$file){
        return "{$info}:$message\n{$file}({$line})";
    }

    private static function htmlConvertEx($e){
        $info = str_replace(__NAMESPACE__.'\\','',get_class($e));
        $message = $e->getMessage();
        $line = $e->getLine();
        $file = $e->getFile();
        $trace = $e->getTraceAsString();
        $trace = explode(PHP_EOL,$trace);
        include YF_PATH . 'tpl' . DS . "exception.php";
        //return "<p><span><h1>{$info}:    $message</h1></span></p>\n<p>{$file}({$line})</p>\n<p>Trace:\n$trace</p>";
    }

    private static function htmlConvertError($message,$info,$line,$file){
        return "<p><h1>{$info}:$message</h1></p>\n <p>{$file}({$line})</p>";
    }
}